package com.xueyi.game.backend.utils;

import com.xueyi.game.backend.config.BaseSqlConfig;
import com.xueyi.game.backend.config.GatewayConfig;
import com.xueyi.game.backend.entity.po.BaseLog;
import com.xueyi.game.backend.entity.po.BaseUser;
import com.xueyi.game.backend.mapper.BackendBaseAccountMapper;
import com.xueyi.game.backend.mapper.BaseLogMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 描述 基础的用户Util
 *
 * @auther 俞烨
 * @date 2022-11-24 10
 */
@Component
@AllArgsConstructor
public class BaseUserUtil {

    private final BaseLogMapper baseLogMapper;
    private final BaseSqlConfig baseSqlConfig;
    private final BackendBaseAccountMapper backendBaseAccountMapper;

    /**
     * 获取当前登录的用户信息
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T findCurrentUser(Class<T> clazz) {

        BaseUser baseUser = GatewayConfig.USER_LOCAL.get();
        return (T) baseUser;
    }

    /**
     * 查询用户的角色集合
     *
     * @param userId
     * @return
     */
    public List<Integer> findUserRoleList(
            Integer userId) {

        return backendBaseAccountMapper.findUserRoleList(userId,
                baseSqlConfig.getUserJoinRoleTableName());
    }

    /**
     * 新增日志
     *
     * @param userId     用户的id
     * @param logContent 日志内容，格式: ${【模块信息】} 用户：${用户名字}${具体的内容}
     *                   比如：【用户管理】用户：俞烨修改了角色的权限
     */
    public void saveLog(String userId, String logContent) {

        BaseLog baseLog = new BaseLog();
        baseLog.setUserId(userId);
        baseLog.setLogContent(logContent);
        baseLogMapper.saveLog(baseLog, baseSqlConfig.getUserLogTableName());
    }
}
